from Common.Base import Base, open_browser
from time import sleep

commodity_url = 'http://localhost:8080/ecshop/admin/index.php'
client_url = 'http://localhost:8080/ecshop/user.php'


class AddProductKindZ(Base):
    """封装表现层制作定位器"""
    forkout_loc = ('css selector', '#panelCloud > div.panel-cross > span')  # 后台弹窗定位器
    login_method_loc = ('class name', 'btn-tab')  # 登后台录方式按钮定位器
    username_loc = ('name', 'username')  # 后台用户名输入框定位
    password_loc = ('name', 'password')  # 后台密码输入框定位
    background_login_loc = ('class name', 'btn-a')  # 后台管理系统登录按钮定位
    cmenu_loc = ('class name', 'icon-goods')  # 商品管理管理定位
    goods_kind_loc = ('id', 'sub-menu-03_category_list')  # 商品分类按钮定位
    add_goods_kind_loc = ("css selector", "body > h1 > a")  # 添加商品按钮定位器
    input_name_loc = (
        "css selector", "#general-table > tbody > tr:nth-child(1) > td:nth-child(2) > input[type=text]")  # 商品种类输入框定位器
    ensure_button = ("css selector", "body > div.main-div > form > div > input:nth-child(1)")  # 确认按钮定位器
    middle_button = ("link text", "移除")  # 移除按钮定位器
    cl_username_loc = ('name', 'username')  # 顾客端登录输入用户名输入框定位器
    cl_password_loc = ('name', 'password')  # 密码输入框定位器
    submit_loc = ('name', 'submit')  # 登录框
    home_loc = ("css selector", "a[class='cur']")  # 首页的输入框
    kind_loc = ("css selector", "#category_tree > div:nth-child(10) > div > a")  # 刚刚增加的商品种类的定位器
    select_loc = ("css selector", "select > option:nth-child(2)")  # 顶级分类选项框下的家用电器选项
    little_kind_loc = (
    "css selector", "#category_tree > div:nth-child(1) > div.cat2-box > div:nth-child(2) > a")  # 家用电器下新添加的商品种类元素定位器

    """封装操作层"""

    def input_username_cl(self, username):
        """
        输入用户名
        :param username: 用户名
        :return:
        """
        self.send_keys(self.cl_username_loc, username)

    def input_password_cl(self, password):
        """
        输入密码
        :param password: 密码
        :return:
        """
        self.send_keys(self.cl_password_loc, password)

    def click_login_cl(self):
        """
        点击登录按钮
        :return:
        """
        self.click(self.submit_loc)

    def take_of(self):
        """
        点击删除弹窗
        :return:
        """
        # 定位弹窗按钮并点击
        self.click(self.forkout_loc)

    def login_method(self):
        """
        点击登录方式按钮进入登录页面
        :return:
        """
        self.click(self.login_method_loc)

    def input_username(self, username):
        """
        输入用户名
        :param username: 用户名
        :return:
        """
        self.send_keys(self.username_loc, username)

    def input_password(self, password):
        """
        输入密码
        :param password: 密码
        :return:
        """
        self.send_keys(self.password_loc, password)

    def click_login(self):
        """
        点击登录按钮
        :return:
        """
        self.click(self.background_login_loc)

    def commodity_manage(self):
        """
        点击进入商品管理,并点击进入商品分类
        :return:
        """
        # 进入frame
        self.driver.switch_to.frame('menu-frame')
        # 定位商品管理并点击商品管理
        self.click(self.cmenu_loc)
        # 定位添加商品并点击添加商品
        self.click(self.goods_kind_loc)

    def click_add_kind(self):
        """
        点击添加商品分类
        :return:
        """
        # 进入frame
        self.driver.switch_to.frame("main-frame")
        # 点击添加商品种类
        self.click(self.add_goods_kind_loc)

    def input_kind_name(self, text):
        """
        输入添加商品种类的文本值
        :text:输入的文本值
        :return:
        """
        self.send_keys(self.input_name_loc, text)

    def select_top_kind(self):
        """
        选择顶级分类下拉框中的家用电器
        :return:
        """
        # 进入frame
        self.click(self.select_loc)

    def click_ensure(self):
        """
        点击确认按钮
        :return:
        """
        self.click(self.ensure_button)

    def get_index(self, text):
        """
        获取刚刚添加的商品种类的索引
        :return:
        """
        # 进入frame
        self.driver.switch_to.frame("main-frame")
        middle_loc = ("css selector", "tbody > tr[align='center'] > td > span >a")
        result = self.find_elements(middle_loc)
        i = 1
        for element in result:
            if element.text == text:
                return i
            else:
                i += 1

    def click_remove(self, text):
        """
        点击刚刚添加的商品种类后面的删除按钮,捕获点击移除后弹出的弹窗,并点击确认按钮
        :return:
        """
        i = self.get_index(text)
        remove_loc = ("css selector", f"table > tbody >tr:nth-of-type({i + 1}) >td:nth-of-type(8) >a:nth-of-type(3)")
        self.click(remove_loc)
        self.get_alert_z()

    def click_home(self):
        """
        点击首页  客户端
        :return:
        """
        self.click(self.home_loc)

    def get_kind_text(self):
        """
        获取首页下面商品分类下的最后一个分类的文本值
        :return:
        """
        return self.get_element_text(self.kind_loc)

    def get_little_kind_text(self):
        """
        获取添加的家用电器下的新分类的元素
        :return:
        """
        result = self.find_element(self.little_kind_loc)
        if result is not False:
            return True
        else:
            return False

    def click_added_kind(self, text):
        """
        点击刚刚添加的商品种类
        :return:
        """
        # 进入frame
        self.driver.switch_to.frame("main-frame")
        kinds_loc = ("link text", f"{text}")
        self.click(kinds_loc)

    def quit_frame(self):
        """
        退出frame
        :return:
        """
        self.driver.switch_to.parent_frame()


if __name__ == '__main__':
    takes = AddProductKindZ(open_browser())
    takes.open_url(commodity_url)
    takes.take_of()
    takes.login_method()
    takes.input_username('root')
    takes.input_password('root123456')
    takes.click_login()
    takes.commodity_manage()
    takes.quit_frame()
    takes.click_add_kind()
    takes.input_kind_name("小型家电")
    takes.select_top_kind()
    takes.click_ensure()
    sleep(3)
    takes.back_z()
    sleep(1)
    # takes.click_remove("小型家电")
    takes.close_browser()
    takes = AddProductKindZ(open_browser())
    takes.open_url(client_url)
    takes.input_username_cl("jerry")
    takes.input_password_cl("123456")
    takes.click_login_cl()
    takes.click_home()
    sleep(4)
    a = takes.get_little_kind_text()
    print(a)

    # takes.click_added_kind("背包")

    # takes.close_browser()
